<script setup lang="ts">
import Grid from "@/components/framework/grid.vue";
import { openExternal } from "@/framework/util/shell";
import { hc } from "@/framework/vue/util/render";
import { CopyOutlined } from "@ant-design/icons-vue";
import ArchiveInstallOperation from "@/pages/tools/components/archive-install-operation.vue";
import { computed, onMounted, ref, shallowRef, watch } from "vue";
import {
  getLibericaBundleTypes,
  getLibericaJavaVersions,
  getLibericaJdkDownloadList,
  getPackageTypeLabel,
  type LibericaJavaVersion,
  type LibericaJdkVersionInfo,
} from "@/pages/tools/java/jdk-api";
import path from "path-browserify-esm";
import type { ToolInstallVersionItem } from "@/pages/tools/common/types";
import { useJdkStore } from "@/pages/tools/java/jdk-store";
import { copyText } from "@/pages/tools/common/util";

const jdkStore = useJdkStore();

const versionList = shallowRef<LibericaJdkVersionInfo[]>([]);
const versionListLoading = ref(false);
const renderVersions = computed(() => {
  return versionList.value.map(it => {
    // https://download.bell-sw.com/java/17.0.12+10/bellsoft-jdk17.0.12+10-windows-amd64.zip
    return {
      id: `liberica-${it.version}`,
      name: it.filename,
      version: it.version,
      // 将github地址转换为官网下载地址
      url: it.downloadUrl.startsWith("https://github.com") ? `https://download.bell-sw.com/java/${it.version}/${it.filename}` : it.downloadUrl,
      edition: "",
    };
  });
});

const featureVersion = ref<number>();
const packageType = ref("jdk");

const baseDataLoading = ref(false);
const javaVersions = shallowRef<LibericaJavaVersion[]>([]);
const javaVersionGroupOptions = computed(() => {
  const lts = [];
  const nonLts = [];
  for (const it of javaVersions.value) {
    const item = { label: it.featureVersion, value: it.featureVersion };
    it.LTS ? lts.push(item) : nonLts.push(item);
  }
  const list = [];
  if (lts.length > 0) list.push({ label: "LTS", options: lts });
  if (nonLts.length > 0) list.push({ label: "非LTS", options: nonLts });
  return list;
});

const packageTypes = shallowRef<string[]>([]);
const packageTypeOptions = computed(() => packageTypes.value.map(it => ({ label: getPackageTypeLabel(it), value: it })));

watch([featureVersion, packageType], async ([version, bundleType]) => {
  versionList.value = [];
  if (version) {
    versionListLoading.value = true;
    versionList.value = await getLibericaJdkDownloadList(version, bundleType);
    versionListLoading.value = false;
  }
});

onMounted(async () => {
  baseDataLoading.value = true;
  try {
    javaVersions.value = await getLibericaJavaVersions();
    packageTypes.value = await getLibericaBundleTypes();
    featureVersion.value = javaVersions.value.find(it => it.LTS)?.featureVersion;
  } finally {
    baseDataLoading.value = false;
  }
});

function getSavePath(it: ToolInstallVersionItem) {
  return path.join(jdkStore.installPath, "liberica", it.name);
}

function isInstalled(it: ToolInstallVersionItem) {
  // TODO 判断jdk版本是否已安装
  return false;
}
</script>

<template>
  <grid rows-preset="two-rows-0">
    <a-form class="mb-16" layout="inline">
      <a-form-item label="Java版本">
        <div class="w-150">
          <a-select placeholder="请选择" v-model:value="featureVersion" :options="javaVersionGroupOptions" :loading="baseDataLoading" />
        </div>
      </a-form-item>
      <a-form-item label="包类型">
        <div class="w-150">
          <a-select v-model:value="packageType" :options="packageTypeOptions" :loading="baseDataLoading" />
        </div>
      </a-form-item>
    </a-form>
    <div class="position-relative">
      <template v-for="it in renderVersions" :key="it.name">
        <div class="tool-install-version-item">
          <a-space>
            <span class="hover-underline" @click.prevent="openExternal(it.url)">{{ it.name }}</span>
            <a-button :icon="hc(CopyOutlined)" size="small" type="text" title="复制下载地址" @click="copyText(it.url)" />
          </a-space>
          <archive-install-operation :id="it.id" :save-path="getSavePath(it)" :is-installed="isInstalled(it)" :download-url="it.url" />
        </div>
      </template>
      <div v-if="versionListLoading" class="abs-fill flex-center">
        <a-spin :spinning="versionListLoading" />
      </div>
    </div>
  </grid>
</template>
